package br.com.rotary.validador;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 *
 * @author laverson
 */
public class EmailValidator implements
        ConstraintValidator<Email, String> {

    @Override
    public void initialize(Email constraintAnnotation) {
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        return validaEmail(value);
    }

    private static boolean validaEmail(String email) {
        if (email == null || email.trim().equals("")) {
            return true;
        } else if (!email.contains("@")) {
            return false;
        }

        Matcher matcher = Pattern.compile("^[\\w-]+(\\.[\\w-]+)*@([\\w-]+\\.)+[a-zA-Z]{2,7}$").matcher(email);

        if (matcher.find()) {
            return true;
        } else {
            return false;
        }
    }
}
